package com.snake.scoreboardlib;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

public class Settings {
	
	public class SettingsLoader {
		private static final String settingFile = "settings.dat";
		private Context context;
		
		public SettingsLoader(Context context)
		{
			this.context = context;
			
		}
		public void Load(String filename)
		{
			FileInputStream fis;
			try {
				fis = context.openFileInput(filename);
			} catch (FileNotFoundException e) {
				return;
			}
			
			InputStreamReader isr = new InputStreamReader(fis);
			char[] buf = new char[255];
			try {
				
				isr.read(buf);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		public void Load()
		{
			Load(settingFile);
		}
	}
	public final String jfnDUID = "duid";
	public String DUID;
	public void Load(Byte[] buf)
	{
		JSONObject jo; 
		try {
			jo = new JSONObject(buf.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		Iterator iter = jo.keys();
	    while(iter.hasNext()){
	        String key = (String)iter.next();
	        String value;
			try {
				value = jo.getString(key);
			} catch (JSONException e) {				
				e.printStackTrace();
				continue;
			}
	        if (key == jfnDUID)
	        {
	        	DUID = value;
	        }
	    }
		
	}

}
